/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui.adapters;


import java.io.File;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.pircbotx.Channel;
import org.pircbotx.DccFileTransfer;
import org.pircbotx.User;

import com.google.code.cubeirc.connection.Connection;
import com.google.code.cubeirc.connection.data.PrivateMessageResponse;
import com.google.code.cubeirc.dialogs.InputDialog;
import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEnum;


public class ChannelPopupListener extends SelectionAdapter {

    @Setter @Getter
	private User sUser;
	@Getter @Setter
	private Channel sChannel;
	
	@Getter @Setter
	private Shell shell;
	
	public ChannelPopupListener(Shell shell, User sUser, Channel sChannel) {	
		setSUser(sUser);
		setSChannel(sChannel);
		setShell(shell);
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		MenuItem mi = (MenuItem)e.getSource();

		if (mi.getText() == "Send private message")
		{
			MessageQueue.addQueue(MessageQueueEnum.MSG_PRIVATE_OUT, new PrivateMessageResponse(Connection.getUserInfo(), sUser, ""));
		}
		if (mi.getText() == "Op")
		{
			Connection.Op(sChannel, sUser);
		}

		if (mi.getText() == "DeOp")
		{
			Connection.Deop(sChannel, sUser);
		}

		if (mi.getText() == "Voice")
		{
			Connection.Voice(sChannel, sUser);
		}

		if (mi.getText() == "DeVoice")
		{
			Connection.Devoice(sChannel, sUser);
		}

		if (mi.getText() == "Kick")
		{
			InputDialog id = new InputDialog(getShell(),SWT.None, getSChannel().getName() , String.format("Enter reason you want kick %s", getSUser().getNick()), "");
			String res = (String)id.open();
			if (res != "")
			{
				Connection.Kick(getSChannel(), getSUser(), res);

			}
		}
		
		if (mi.getText() == "DCC Send file")
		{
			FileDialog fd = new  FileDialog(getShell(), SWT.OPEN);
			fd.setText(String.format("Select file you want send to %s", getSUser().getNick()));
			String fsel = fd.open();
			
			if (fsel != null || fsel != "")
			{
				//DccFileTransfer ft =  Connection.getIrcclient().dccSendFile(new File(fsel), getSUser(), 2000);
		        //MessageQueue.addQueue(MessageQueueEnum.DCC_FILE_OUTCOMING, getSUser(), null, null, ft.getUser().getBot().getUserBot(), ft);		        				
			}
		}
		



		super.widgetSelected(e);
	}



}

